#!/usr/bin/env tcsh

##################################
# L2_setup 			 #
# Last Modified Date:   17/07/13 #
##################################

############################
# User defined environment #
############################

setenv L2_BASE_DIR "/home/rmb/devel/FRODOSpec/frodo-l2-pipeline"				# rmb/laptop
#setenv L2_BASE_DIR "/space/home/eng/frodo_build"						# lt/ltdevsrv
#setenv L2_BASE_DIR "/usr/local/bin/frodo_l2_pipeline/trunk"					# lt/lt-archive/deploy

setenv L2_SUCCESS_LOG_PATH "/home/rmb/devel/FRODOSpec/frodo-l2-pipeline/logs/L2_success.log"	# rmb/laptop
#setenv L2_SUCCESS_LOG_PATH "/home/data/rmb/L2_success.log"					# lt/lt-archive

setenv L2_FAIL_LOG_PATH "/home/rmb/devel/FRODOSpec/frodo-l2-pipeline/logs/L2_fail.log"		# rmb/laptop
#setenv L2_FAIL_LOG_PATH "/home/data/rmb/L2_fail.log"						# lt/lt-archive

setenv LD_LIBRARY_PATH $LD_LIBRARY_PATH":/usr/lib:/usr/lib/gsl"					# all

setenv L2_EXT_SOFTWARE_PATH_IM "/usr/bin"							# all
setenv L2_EXT_SOFTWARE_PATH_GNUPLOT "/usr/bin"							# all

# DO NOT ALTER PAST THIS LINE
# ----------------------------------------------------------------------------------- #

##########################
# Set up shell variables #
##########################

setenv L2_BIN_DIR $L2_BASE_DIR/bin
setenv L2_LIB_DIR $L2_BASE_DIR/lib
setenv L2_CONFIG_DIR $L2_BASE_DIR/config
setenv L2_SRC_DIR $L2_BASE_DIR/src
setenv L2_SCRIPT_DIR $L2_BASE_DIR/scripts
setenv L2_MAN_DIR $L2_BASE_DIR/man

setenv L2_REFERENCE_ARCS_DIR $L2_CONFIG_DIR/reference_arcs
setenv L2_LOOKUP_TABLES_DIR $L2_CONFIG_DIR/lookup_tables

setenv L2_ERROR_CODES_FILE $L2_CONFIG_DIR/ERROR_CODES
setenv L2_FITS_KEYS_TO_OMIT_FILE $L2_CONFIG_DIR/FITS_KEYS_TO_OMIT

setenv L2_HEADER_FILE $L2_MAN_DIR/HEADER
setenv L2_FRFS_BLURB_FILE $L2_MAN_DIR/FRODO_RED_FINDPEAKS_SIMPLE
setenv L2_FRFSC_BLURB_FILE $L2_MAN_DIR/FRODO_RED_FINDPEAKS_SIMPLE_CLEAN
setenv L2_FRT_BLURB_FILE $L2_MAN_DIR/FRODO_RED_TRACE
setenv L2_FRES_BLURB_FILE $L2_MAN_DIR/FRODO_RED_EXTRACT_SIMPLE
setenv L2_FRA_BLURB_FILE $L2_MAN_DIR/FRODO_RED_ARCFIT
setenv L2_FRCT_BLURB_FILE $L2_MAN_DIR/FRODO_RED_CORRECT_THROUGHPUT
setenv L2_FRR_BLURB_FILE $L2_MAN_DIR/FRODO_RED_REBIN
setenv L2_FRS_BLURB_FILE $L2_MAN_DIR/FRODO_RED_SUBSKY
setenv L2_FRRF_BLURB_FILE $L2_MAN_DIR/FRODO_RED_REFORMAT
setenv L2_FAN_BLURB_FILE $L2_MAN_DIR/FRODO_AUX_NUMEXTS
setenv L2_FAS_BLURB_FILE $L2_MAN_DIR/FRODO_AUX_SPEC2TSV
setenv L2_FAP_BLURB_FILE $L2_MAN_DIR/FRODO_AUX_PEAKFINDER

setenv L2_EXEC_BLURB_FILE $L2_MAN_DIR/L2_EXEC

setenv L2_VERSION_FILE $L2_MAN_DIR/VERSION

################
# Set up shell #
################

limit stacksize unlimited
source $L2_SCRIPT_DIR/L2_aliases

##############################
# Set permissions on scripts #
##############################

chmod +x $L2_SCRIPT_DIR/*

###########################
# Update man/VERSION file #
###########################

set author = `svn info $L2_BASE_DIR | grep "Last Changed Author" | awk -F "Author:" '{print $2}'`
set rev = `svn info $L2_BASE_DIR | grep "Last Changed Rev" | awk -F "Rev:" '{print $2}'`
set date = `svn info $L2_BASE_DIR | grep "Last Changed Date" | awk -F "Date:" '{print $2}'`

set author = "Last Changed Author:\t$author"
set rev = "Last Changed Revision:\t$rev"
set date = "Last Changed Date:\t$date"

echo $author > $L2_VERSION_FILE
echo $rev >> $L2_VERSION_FILE	
echo $date >> $L2_VERSION_FILE

######################################################################################################
# Create time log file ($L2_TIME_LOG_PATH) and fail log file ($L2_FAIL_LOG_PATH) if it doesn't exist #
######################################################################################################

if (! -e $L2_SUCCESS_LOG_PATH) then

	echo "L2_SUCCESS_LOG\n\nStart Timestamp,Target File,Arc File,Continuum File,Configuration,Execution Time (s),Number of filled HDUs,Warning Message" > $L2_SUCCESS_LOG_PATH

endif

if (! -e $L2_FAIL_LOG_PATH) then 

	echo "L2_FAIL_LOG\n\nStart Timestamp,Target File,Arc File,Continuum File,Error Message" > $L2_FAIL_LOG_PATH

endif

exit(0)
